package net.mingsoft.cmsMessage.action;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.util.ObjectUtil;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import net.mingsoft.base.entity.ResultData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.ModelMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;
import net.mingsoft.cmsMessage.biz.ICmsScheduleBiz;
import net.mingsoft.cmsMessage.entity.CmsScheduleEntity;
import net.mingsoft.base.util.JSONObject;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.base.filter.DateValueFilter;
import net.mingsoft.base.filter.DoubleValueFilter;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import org.apache.commons.lang3.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
/**
 * 日程安排管理控制层
 * @author ym
 * 创建日期：2022-1-2 22:08:54<br/>
 * 历史修订：<br/>
 */
@Api(value = "日程安排接口")
@Controller("cmsmessageCmsScheduleAction")
@RequestMapping("/${ms.manager.path}/cmsMessage/cmsSchedule")
public class CmsScheduleAction extends BaseAction{


	/**
	 * 注入日程安排业务层
	 */
	@Autowired
	private ICmsScheduleBiz cmsScheduleBiz;

	/**
	 * 返回主界面index
	 */
	@GetMapping("/index")
	public String index(HttpServletResponse response,HttpServletRequest request) {
		return "/cmsMessage/cms-schedule/index";
	}

	/**
	 * 查询日程安排列表
	 * @param cmsSchedule 日程安排实体
	 */
	@ApiOperation(value = "查询日程安排列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "arrangeStatus", value = "状态", paramType = "query"),
			@ApiImplicitParam(name = "arrangeTime", value = "安排时间", paramType = "query"),
			@ApiImplicitParam(name = "content", value = "安排内容", paramType = "query"),
			@ApiImplicitParam(name = "leader", value = "领导人", paramType = "query"),
    })
	@RequestMapping(value ="/list",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultData list(@ModelAttribute @ApiIgnore CmsScheduleEntity cmsSchedule,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model,BindingResult result) {
		BasicUtil.startPage();
		List cmsScheduleList = null;
		if ( cmsSchedule.getSqlWhere() != null){
			cmsScheduleList = cmsScheduleBiz.query(cmsSchedule);
		} else {
			QueryWrapper queryWrapper = new QueryWrapper<CmsScheduleEntity>();
			if(StrUtil.isNotEmpty(cmsSchedule.getLeader())){
				queryWrapper.like("LEADER",cmsSchedule.getLeader().trim());
			}
			queryWrapper.eq(StrUtil.isNotEmpty(cmsSchedule.getArrangeStatus()),"ARRANGE_STATUS",cmsSchedule.getArrangeStatus());
			cmsScheduleList = cmsScheduleBiz.list(queryWrapper);
		}
		return ResultData.build().success(new EUListBean(cmsScheduleList,(int)BasicUtil.endPage(cmsScheduleList).getTotal()));
	}

	/**
	 * 返回编辑界面cmsSchedule的form
	 */
	@GetMapping("/form")
	public String form(@ModelAttribute CmsScheduleEntity cmsSchedule,HttpServletResponse response,HttpServletRequest request,ModelMap model) {
		return "/cmsMessage/cms-schedule/form";
	}


	/**
	 * 获取日程安排
	 * @param cmsSchedule 日程安排实体
	 */
	@ApiOperation(value = "获取日程安排列表接口")
    @ApiImplicitParam(name = "id", value = "主键ID", required =true,paramType="query")
	@GetMapping("/get")
	@ResponseBody
	public ResultData get(@ModelAttribute @ApiIgnore CmsScheduleEntity cmsSchedule,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model) {
		if (cmsSchedule.getId()==null) {
			return ResultData.build().error();
		}
		CmsScheduleEntity _cmsSchedule = (CmsScheduleEntity)cmsScheduleBiz.getById(cmsSchedule.getId());
		return ResultData.build().success(_cmsSchedule);
	}


	/**
	* 保存日程安排
	* @param cmsSchedule 日程安排实体
	*/
	@ApiOperation(value = "保存日程安排列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "arrangeStatus", value = "状态", required =true, paramType = "query"),
			@ApiImplicitParam(name = "arrangeTime", value = "安排时间", required =true, paramType = "query"),
			@ApiImplicitParam(name = "content", value = "安排内容", required =true, paramType = "query"),
			@ApiImplicitParam(name = "leader", value = "领导人", required =true, paramType = "query"),
	})
	@PostMapping("/save")
	@ResponseBody
	@LogAnn(title = "保存日程安排", businessType = BusinessTypeEnum.INSERT)
	@RequiresPermissions("cmsMessage:cmsSchedule:save")
	public ResultData save(@ModelAttribute @ApiIgnore CmsScheduleEntity cmsSchedule, HttpServletResponse response, HttpServletRequest request) {
		//验证状态的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getArrangeStatus())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("arrange.status")));
		}
		//验证安排时间的值是否合法
		if (cmsSchedule.getArrangeTime()==null) {
			return ResultData.build().error(getResString("err.empty", this.getResString("arrange.time")));
		}
		//验证安排内容的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getContent())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("content")));
		}
		//验证领导人的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getLeader())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("leader")));
		}
		if (!StringUtil.checkLength(cmsSchedule.getLeader()+"", 0, 255)) {
			return ResultData.build().error(getResString("err.length", this.getResString("leader"), "0", "255"));
		}
		cmsScheduleBiz.save(cmsSchedule);
		return ResultData.build().success(cmsSchedule);
	}

	/**
     *  删除日程安排
     *
	 * @param cmsSchedules 日程安排实体
	 */
	@ApiOperation(value = "批量删除日程安排列表接口")
	@PostMapping("/delete")
	@ResponseBody
	@LogAnn(title = "删除日程安排", businessType = BusinessTypeEnum.DELETE)
	@RequiresPermissions("cmsMessage:cmsSchedule:del")
	public ResultData delete(@RequestBody List<CmsScheduleEntity> cmsSchedules,HttpServletResponse response, HttpServletRequest request) {
	    List<String> ids = (List)cmsSchedules.stream().map((p) -> {return p.getId();}).collect(Collectors.toList());
		return this.cmsScheduleBiz.removeByIds(ids) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
		}

	/**
	 *	更新日程安排列表
	 *
	 * @param cmsSchedule 日程安排实体
	 */
	 @ApiOperation(value = "更新日程安排列表接口")
	 @ApiImplicitParams({
    		@ApiImplicitParam(name = "arrangeStatus", value = "状态", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "arrangeTime", value = "安排时间", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "content", value = "安排内容", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "leader", value = "领导人", required =true, paramType = "query"),
	})
	@PostMapping("/update")
	@ResponseBody
	@LogAnn(title = "更新日程安排", businessType = BusinessTypeEnum.UPDATE)
	@RequiresPermissions("cmsMessage:cmsSchedule:update")
	public ResultData update(@ModelAttribute @ApiIgnore CmsScheduleEntity cmsSchedule, HttpServletResponse response,
			HttpServletRequest request) {
		//验证状态的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getArrangeStatus())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("arrange.status")));
		}
		//验证安排时间的值是否合法
		if (cmsSchedule.getArrangeTime()==null) {
			return ResultData.build().error(getResString("err.empty", this.getResString("arrange.time")));
		}
		//验证安排内容的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getContent())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("content")));
		}
		//验证领导人的值是否合法
		if (StringUtils.isBlank(cmsSchedule.getLeader())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("leader")));
		}
		if (!StringUtil.checkLength(cmsSchedule.getLeader()+"", 0, 255)) {
			return ResultData.build().error(getResString("err.length", this.getResString("leader"), "0", "255"));
		}
			cmsScheduleBiz.updateById(cmsSchedule);
		return ResultData.build().success(cmsSchedule);
	}


}
